package com.biubiu.biubiuexam.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.biubiu.biubiuexam.common.dto.*;
import com.biubiu.biubiuexam.common.lang.Result;
import com.biubiu.biubiuexam.services.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * The type Teacher controller.
 */
@RestController
public class TeacherController {

    private final TeacherService teacherService;

    /**
     * Instantiates a new Teacher controller.
     *
     * @param teacherService the teacher service
     */
    @Autowired
    public TeacherController(TeacherService teacherService) {
        this.teacherService = teacherService;
    }

    /**
     * Gets all clazz of student.
     *
     * @return the all clazz of student
     */
    @GetMapping("/teacher/getMyAllClasses")
    public Result getAllClazzOfStudent() {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录！", null);
        }

        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.getAllClazzOfTeacher(StpUtil.getLoginIdAsLong());
    }

    /**
     * Create clazz result.
     *
     * @param createClazzDto the create clazz dto
     * @return the result
     */
    @PostMapping("/teacher/createClass")
    public Result createClazz(@Valid @RequestBody TeacherCreateClazzDto createClazzDto) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录！", null);
        }

        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.createClazz(StpUtil.getLoginIdAsLong(), createClazzDto);
    }

    /**
     * Del class result.
     *
     * @param code the code
     * @return the result
     */
    @PostMapping("/teacher/delClass")
    public Result delClass(@RequestBody String code) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录！", null);
        }

        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.delClazz(StpUtil.getLoginIdAsLong(), code);
    }

    /**
     * Edit clazz result.
     *
     * @param clazzDto the clazz dto
     * @return the result
     */
    @PostMapping("/teacher/editClass")
    public Result editClazz(@Valid @RequestBody TeacherEditClazzDto clazzDto) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录！", null);
        }

        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.editClazz(StpUtil.getLoginIdAsLong(), clazzDto);
    }

    /**
     * Create exam result.
     *
     * @param examDto the exam dto
     * @return the result
     */
    @PostMapping("/teacher/createExam")
    public Result createExam(@Valid @RequestBody TeacherCreateExamDto examDto) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.createExam(StpUtil.getLoginIdAsLong(), examDto);
    }

    /**
     * Gets class info.
     *
     * @param link the link
     * @return the class info
     */
    @GetMapping("/teacher/getClassInfo")
    public Result getClassInfo(@RequestParam String link) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.getClazzInfo(link);
    }

    /**
     * Edit exam result.
     *
     * @param teacherEditExamDto the teacher edit exam dto
     * @return the result
     */
    @PostMapping("/teacher/editExam")
    public Result editExam(@Valid @RequestBody TeacherEditExamDto teacherEditExamDto) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.editExam(StpUtil.getLoginIdAsLong(), teacherEditExamDto);
    }

    /**
     * Del exam result.
     *
     * @param link the link
     * @return the result
     */
    @PostMapping("/teacher/delExam")
    public Result delExam(@Valid @RequestBody String link) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.delExam(StpUtil.getLoginIdAsLong(), link);
    }

    /**
     * Create exam paper result.
     *
     * @param teacherCreateExamPaperDto the teacher create exam paper dto
     * @return the result
     */
    @PostMapping("/teacher/createExamPaper")
    public Result createExamPaper(@Valid @RequestBody TeacherCreateExamPaperDto teacherCreateExamPaperDto) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.createExamPaper(StpUtil.getLoginIdAsLong(), teacherCreateExamPaperDto);
    }

    /**
     * Gets all exam paper.
     *
     * @return the all exam paper
     */
    @GetMapping("/teacher/getMyAllExamPaper")
    public Result getAllExamPaper() {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.getMyAllExamPaper(StpUtil.getLoginIdAsLong());
    }

    /**
     * Edit exam paper result.
     *
     * @param teacherEditExamPaperDto the teacher edit exam paper dto
     * @return the result
     */
    @PostMapping("/teacher/editExamPaper")
    public Result editExamPaper(@Valid @RequestBody TeacherEditExamPaperDto teacherEditExamPaperDto) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.editExamPaper(StpUtil.getLoginIdAsLong(), teacherEditExamPaperDto);
    }

    /**
     * Del exam paper result.
     *
     * @param link the link
     * @return the result
     */
    @PostMapping("/teacher/delExamPaper")
    public Result delExamPaper(@Valid @RequestBody String link) {
        if (!StpUtil.isLogin()) {
            return Result.fail(402, "未登录", null);
        }
        if (!StpUtil.hasRole("teacher")) {
            return Result.fail(405, "您不是教师！", null);
        }

        return teacherService.delExamPaper(StpUtil.getLoginIdAsLong(), link);
    }

}
